<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <title>设置角色</title>
    <script th:replace="common/head::static"></script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div id="layuiTransferSysRole"></div>
        </div>
    </div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn" lay-transferActive="getData">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" lay-transferActive="reload">重置</button>
    </div>
</div>

<script th:src="@{/common/js/common.js}"></script>
<script th:inline="javascript">

    const sysUserId = [[${sysUserId}]];

    const checkedIds = [[${checkedIds}]];

    layui.use(['transfer', 'layer', 'util'], function () {
        const $ = layui.$
            , transfer = layui.transfer
            , layer = layui.layer
            , util = layui.util;

        Common.ajaxGetNoParams(ctx + '/sysRole/findAll', function (res) {
            if (res.value === 200) {
                transfer.render({
                    elem: '#layuiTransferSysRole'
                    , title: ['可选角色', '已选角色']
                    , data: res.data
                    , parseData: function (result) {
                        return {
                            "value": result.id //数据值
                            , "title": result.name //数据标题
                            , "disabled": result.disabled  //是否禁用
                            , "checked": result.checked //是否选中
                        }
                    }
                    , value: checkedIds
                    , showSearch: true
                    , width: 200
                    , height: 300
                    , id: "uniqueId"
                });
            }
        });
        util.event('lay-transferActive', {
            getData: function (othis) {
                const getData = transfer.getData('uniqueId');

                const sysRoleIds = [];
                $.each(getData, function (index, data) {
                    sysRoleIds.push(data.value);
                });
                if (sysRoleIds.length === 0) {
                    layer.msg('未勾选任何角色', function () {
                        return false;
                    });
                } else {
                    const params = {
                        sysRoleIds: sysRoleIds.join(","),
                        sysUserId: sysUserId
                    }
                    Common.ajaxPut(ctx + '/sysUser/setSysRole', params, function (res) {
                        if (res.value === 200) {
                            layer.msg(res.message, {icon: 1, time: 1000}, function () {
                                Common.closeFrame();
                            });
                        } else {
                            layer.msg(res.message, {icon: 2, time: 2000});
                        }
                    });
                }
            }
            , reload: function () {
                transfer.reload('uniqueId', {});
            }
        });
    });

</script>
</body>
</html>